package com.hand.wfive.dialog

import android.app.Dialog
import android.content.Context
import android.graphics.Point
import android.os.Bundle
import android.view.View
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import com.hand.wfive.R
import com.hand.wfive.util.click.singleClick
import kotlinx.android.synthetic.main.dialog_common_tips.tvContent
import kotlinx.android.synthetic.main.dialog_common_tips.tvLeftBtn
import kotlinx.android.synthetic.main.dialog_common_tips.tvRightBtn
import kotlinx.android.synthetic.main.dialog_msg.*
import org.jetbrains.anko.backgroundResource
import org.jetbrains.anko.textColor

class MsgDialog(
    context: Context,
    msg: String,
    title: String = "提示",
    negative: String = "取消",
    positive: String = "确定",
    showNegative: Boolean = true,
    positiveBg: Int = R.drawable.btn_dialog_positive,
    event: (Int) -> Unit = { println(it) },
) : Dialog(context, R.style.CustomDialog) {
    init {
        setContentView(R.layout.dialog_msg)

        setCancelable(false)
        setCanceledOnTouchOutside(false)
        tvTitle.text = title
        tvMsg.text = msg

        if (showNegative) {
            tvNegative.text = negative
            tvNegative.singleClick {
                dismiss()
                event(0)
            }
        } else {
            tvNegative.isVisible = false
        }
        tvPositive.setBackgroundResource(positiveBg)
        tvPositive.text = positive
        tvPositive.singleClick {
            dismiss()
            event(1)
        }
    }
}

class CommonTipsDialog(
    context: Context,
    private var title: String = "提示",
    private var content: String,
    private var leftText: String = "取消",
    private var leftTextColor: Int = R.color.black,
    private var leftBtnBgColor: Int = R.drawable.shape_white_left_bottom_solid8,
    private var rightText: String = "确认",
    private var rightTextColor: Int = R.color.black,
    private var rightBtnBgColor: Int = R.drawable.shape_white_right_bottom_solid8,
    private var mListener: TipListener?,
) : Dialog(
    context, R.style.CustomDialog
) {

    companion object {
        fun showDialog(
            context: Context, title: String = "提示",
            content: String,
            leftText: String = "取消",
            leftTextColor: Int = R.color.black,
            leftBtnBgRes: Int = R.drawable.shape_white_left_bottom_solid8,
            rightText: String = "确认",
            rightTextColor: Int = R.color.black, listener: TipListener?,
            rightBtnBgRes: Int = R.drawable.shape_white_right_bottom_solid8,
        ) {
            CommonTipsDialog(
                context,
                title,
                content,
                leftText,
                leftTextColor,
                leftBtnBgRes,
                rightText,
                rightTextColor,
                rightBtnBgRes,
                listener
            ).show()
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_common_tips)
        setWidth()
        //按空白处不能取消动画
        setCanceledOnTouchOutside(false)
        setCancelable(false)
        //初始化界面控件的事件
        initViewAndData()
    }

    private fun initViewAndData() {
        tvTitle.text = title
        tvContent.text = content
        tvLeftBtn.text = leftText
        tvLeftBtn.textColor = ContextCompat.getColor(context, leftTextColor)
        tvLeftBtn.backgroundResource = leftBtnBgColor
        tvRightBtn.text = rightText
        tvRightBtn.textColor = ContextCompat.getColor(context, rightTextColor)
        tvRightBtn.backgroundResource = rightBtnBgColor
        //设置取消按钮被点击后，向外界提供监听
        tvLeftBtn.setOnClickListener { v: View? ->
            mListener?.doLeftEvent(this@CommonTipsDialog)
            dismiss()
        }
        tvRightBtn.setOnClickListener { v: View? ->
            mListener?.doRightEvent(this@CommonTipsDialog)
            dismiss()
        }
    }

    private fun setWidth() {
        //设置宽度
        val m = window?.windowManager
        val d = m?.defaultDisplay
        val p = window?.attributes
        val size = Point()
        d?.getSize(size)
        p?.width = (size.x * 0.75).toInt() //设置dialog的宽度为当前手机屏幕的宽度*0.8
        window?.attributes = p
    }

    interface TipListener {
        fun doLeftEvent(dialog: CommonTipsDialog) {}
        fun doRightEvent(dialog: CommonTipsDialog)
    }
}